package com.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.entity.Answers;
import com.boot.service.AnswersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.boot.bean.R;

import com.boot.service.QuestionsService;
import com.boot.entity.Questions;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 问题表，存储测验考试中的问题 前端控制器
 * </p>
 *
 * @author
 * @since 2025-02-09
 */
@RestController
@RequestMapping("/questions")
public class QuestionsController {
    @Resource
    private QuestionsService questionsService;
    @Autowired
    private AnswersService answersService;

    //新增或者更新
    @PostMapping
    public R save(@RequestBody Questions questions) {
        questionsService.saveOrUpdate(questions);
        for (Answers answer : questions.getAnswers()) {
            answer.setQuestionId(questions.getId());
            answersService.saveOrUpdate(answer);
        }
        return R.ok();
    }

    //删除
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id) {
        questionsService.removeById(id);
        return R.ok();
    }

    @PostMapping("/del/batch")
    public R deleteBatch(@RequestBody List<Integer> ids) {//批量删除
        questionsService.removeByIds(ids);
        return R.ok();
    }

    //查询所有数据
    @GetMapping
    public R findAll(Integer courseId) {
        QueryWrapper<Questions> queryWrapper = new QueryWrapper<>();
        if (courseId != null && courseId > 0) {
            queryWrapper.eq("course_id", courseId);
        }
        List<Questions> list = questionsService.list(queryWrapper);
        for (Questions questions : list) {
            QueryWrapper<Answers> q = new QueryWrapper<>();
            q.eq("question_id", questions.getId());
            questions.setAnswers(answersService.list(q));
        }
        return R.ok(list);
    }

    @GetMapping("/{id}")
    public R findOne(@PathVariable Integer id) {
        Questions questions = questionsService.getById(id);
        QueryWrapper<Answers> q = new QueryWrapper<>();
        q.eq("question_id", id);
        questions.setAnswers(answersService.list(q));
        return R.ok(questions);
    }


    @GetMapping("/page")
    public R findPage(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        QueryWrapper<Questions> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        IPage<Questions> page = questionsService.page(new Page<>(pageNum, pageSize), queryWrapper);
        for (Questions record : page.getRecords()) {
            QueryWrapper<Answers> q = new QueryWrapper<>();
            q.eq("question_id", record.getId());
            record.setAnswers(answersService.list(q));
        }
        return R.ok(page);
    }
}

